//
//  ZMSearchController.m
//  CenturyCity
//
//  Created by beisheng03 on 2017/12/20.
//  Copyright © 2017年 beisheng. All rights reserved.
//

#import "ZMSearchController.h"
#import "ZMSearchNavView.h"
#import "SearchOneSectionCell.h"
#import "ZMSearchHeaderVeiw.h"
#import "SearchYellowPaygVC.h"
#import "BaseViewController.h"
#import "SearchEmploymentVC.h"
#import "SearchRentHouseVC.h"
#import "SearchCategoryListVC.h"
#import "SearchLifeController.h"
@interface ZMSearchController ()<ZMSearchNavViewDelegate>
/** <#note#> */
@property (nonatomic, weak) ZMSearchNavView *headerView;

/** <#note#> */
//@property (nonatomic, strong) NSArray *oneSectionTitles;
@end

@implementation ZMSearchController

//- (NSArray *)oneSectionTitles{
//
//    if (!_oneSectionTitles) {
//        _oneSectionTitles = @[@"金融",@"理财",@"新能源",@"软件开发",@"金融",@"理财",@"新能源",@"软件开发",@"金融",@"理财",@"新能源",@"软件开发",@"金融",@"理财",@"新能源",@"软件开发",@"金融",@"理财",@"新能源",@"软件开发",@"金融",@"理财",@"新能源",@"软件开发",@"金融",@"理财",@"新能源",@"软件开发"];
//    }
//
//
//
//    return _oneSectionTitles;
//}




- (ZMSearchNavView *)headerView{
    
    if (!_headerView) {
        
        ZMSearchNavView *temp = (ZMSearchNavView *)[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([ZMSearchNavView class]) owner:nil options:nil].firstObject;
        
        temp.delegate = self;
        temp.type = self.type;
        
        CGFloat viewH = IPhoneX ? 88 : 64;
        
        temp.frame = CGRectMake(0, 0, KScreenW, viewH);
        //1 黄页  2 招聘  3 商品 4租房
        if ([self.type isEqualToString:@"1"]) {
            
            temp.searchTextField.placeholder = @"输入企业名称或行业类别";

        } else if ([self.type isEqualToString:@"2"]){
            
            temp.searchTextField.placeholder = @"输入企业名称或职位类别";

        } else if ([self.type isEqualToString:@"3"]){
            
            temp.searchTextField.placeholder = @"输入商品名称搜索";

            
        }else  if ([self.type isEqualToString:@"4"]){
            
            temp.searchTextField.placeholder = @"请输入小区或街道名称";

            
        }else  if ([self.type isEqualToString:@"5"]){
            
            temp.searchTextField.placeholder = @"请输入文章标题";
            
            
        }else{
            
            temp.searchTextField.placeholder = @"输入关键字搜索";

            
        }
        
        
        _headerView = temp;
        
        [self.view addSubview:temp];
        
    }
    
    return _headerView;
}


- (void)reternBtnDidClick:(NSString *)keyword{
    
 
       //1 黄页  2 招聘  3 商品 4租房
    if ([self.type isEqualToString:@"1"]) {
        
        
        SearchYellowPaygVC *contr = [[SearchYellowPaygVC alloc]init];
        
        contr.keyword = keyword;
        
        [self.navigationController pushViewController:contr animated:YES];
        
        
    } else if ([self.type isEqualToString:@"2"]){
        
        SearchEmploymentVC *contr = [[SearchEmploymentVC alloc]init];
        
        contr.keyword = keyword;

        [self.navigationController pushViewController:contr animated:YES];
        
    } else if ([self.type isEqualToString:@"3"]){
        
        
        SearchCategoryListVC *contr = [[SearchCategoryListVC alloc]init];
        
        contr.keyword = keyword;
        
        contr.navigationItem.title = keyword;
        
        [self.navigationController pushViewController:contr animated:YES];
        
        
    }else  if ([self.type isEqualToString:@"4"]){
        
        SearchRentHouseVC *contr = [[SearchRentHouseVC alloc]init];
        
        contr.keyword = keyword;
        
        
        contr.navigationItem.title = keyword;
        
        [self.navigationController pushViewController:contr animated:YES];
        
        
    }else if([self.type isEqualToString:@"5"]){
        
        SearchLifeController *contr = [[SearchLifeController alloc]init];
        
        contr.keyword = keyword;
        
        
        contr.navigationItem.title = keyword;
        
        [self.navigationController pushViewController:contr animated:YES];
        
        
    }else{
        
        BaseViewController *contr = [[BaseViewController alloc]init];
        
        
        [self.navigationController pushViewController:contr animated:YES];
        
        
    }
    
    
    
   
    
    
  [self.collectionView reloadData];
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.collectionView.alwaysBounceVertical = YES;

    self.collectionView.backgroundColor = [UIColor whiteColor];

    CGFloat navH = IPhoneX ? 88 : 64;
    CGFloat tabbarH = IPhoneX ? 34 : 0;
    
    if (@available(iOS 11.0, *)) {
        //        contentInsetAdjustmentBehavior属性也是用来取代automaticallyAdjustsScrollViewInsets属性的，推荐使用这种方式。
        self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        
        self.collectionView.contentInset = UIEdgeInsetsMake(navH, 0, tabbarH, 0);
        
    }else{
        
        self.collectionView.contentInset = UIEdgeInsetsMake(navH, 0, tabbarH, 0);
        
    }
    
    
    
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([SearchOneSectionCell class]) bundle:nil] forCellWithReuseIdentifier:[SearchOneSectionCell identifierStr]];
    
    
    //注册分区头标题
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([ZMSearchHeaderVeiw class]) bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:[ZMSearchHeaderVeiw identifierStr]];
    
    
    [self.collectionView registerClass:[UICollectionReusableView class]
              forSupplementaryViewOfKind:UICollectionElementKindSectionFooter
                     withReuseIdentifier:@"CollectionViewFooterView"];
    
    self.headerView.backgroundColor = themeColor;
    
    
    [self.headerView.searchTextField becomeFirstResponder];

    
    // Do any additional setup after loading the view.
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    
    
    
}



- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:YES];
    
    
    [self.headerView.searchTextField resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    if ([self.type isEqualToString:@"1"]) {
        if (myViewModel.searchHistory.searchHistoryStrs.count > 0) {
            
            return 2;
        }else{
            
            return 1;
        }
    }else if ([self.type isEqualToString:@"2"]) {
        
        if (myViewModel.searchHistory.searchHistoryStrs.count > 0) {
            
            return 2;
        }else{
            
            return 1;
        }
    } else {
        if (myViewModel.searchHistory.searchHistoryStrs.count > 0) {
            
            return 1;
        }else{
            
            return 0;
        }
    }
    

    
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if ([self.type isEqualToString:@"1"]) {
        
        if (myViewModel.searchHistory.searchHistoryStrs.count > 0) {
            
            if (section == 0) {
                
                return myViewModel.parameterModel.yellowPagesClass.count;
                
            } else {
                
                return myViewModel.searchHistory.searchHistoryStrs.count;

            }
            
            
        }else{
            
            return myViewModel.parameterModel.yellowPagesClass.count;
        }
    }else if ([self.type isEqualToString:@"2"]) {
        
        if (myViewModel.searchHistory.searchHistoryStrs.count > 0) {
            
            if (section == 0) {
                
                return myViewModel.parameterModel.positionClass.count;
                
            } else {
                
                return myViewModel.searchHistory.searchHistoryStrs.count;
                
            }
            
            
        }else{
            
            return myViewModel.parameterModel.positionClass.count;
        }
    } else {
        
        if (myViewModel.searchHistory.searchHistoryStrs.count > 0) {
            
            return myViewModel.searchHistory.searchHistoryStrs.count;
            
        }else{
            
            return 0;
        }
    }
    

   
    
    
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.type isEqualToString:@"1"]) {
        
        
        if (myViewModel.searchHistory.searchHistoryStrs.count > 0) {
            
            if (indexPath.section == 0) {
                
                SearchOneSectionCell *cell = (SearchOneSectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:[SearchOneSectionCell identifierStr] forIndexPath:indexPath];
                
                
                cell.title = myViewModel.parameterModel.yellowPagesClass[indexPath.item].name;
                
                
                return cell;
                
            } else {
                
                SearchOneSectionCell *cell = (SearchOneSectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:[SearchOneSectionCell identifierStr] forIndexPath:indexPath];
                
                
                cell.title = [myViewModel.searchHistory.searchHistoryStrs[indexPath.item] substringFromIndex:1];
                
                
                return cell;
            }
            
            
        }else{
            
            SearchOneSectionCell *cell = (SearchOneSectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:[SearchOneSectionCell identifierStr] forIndexPath:indexPath];
            
            
            cell.title = myViewModel.parameterModel.yellowPagesClass[indexPath.item].name;
            
            
            return cell;
            
            
        }
        
        
        

        
    } else  if ([self.type isEqualToString:@"2"]) {
        
        
        if (myViewModel.searchHistory.searchHistoryStrs.count > 0) {
            
            if (indexPath.section == 0) {
                
                SearchOneSectionCell *cell = (SearchOneSectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:[SearchOneSectionCell identifierStr] forIndexPath:indexPath];
                
                
                cell.title = myViewModel.parameterModel.positionClass[indexPath.item].name;
                
                
                return cell;
                
            } else {
                
                SearchOneSectionCell *cell = (SearchOneSectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:[SearchOneSectionCell identifierStr] forIndexPath:indexPath];
                
                
                cell.title = [myViewModel.searchHistory.searchHistoryStrs[indexPath.item] substringFromIndex:1];
                
                
                return cell;
            }
            
            
        }else{
            
            SearchOneSectionCell *cell = (SearchOneSectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:[SearchOneSectionCell identifierStr] forIndexPath:indexPath];
            
            
            cell.title = myViewModel.parameterModel.positionClass[indexPath.item].name;
            
            
            return cell;
            
            
        }
        
        
        
        
        
    } else {
        
        SearchOneSectionCell *cell = (SearchOneSectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:[SearchOneSectionCell identifierStr] forIndexPath:indexPath];
        
        
        cell.title = [myViewModel.searchHistory.searchHistoryStrs[indexPath.item] substringFromIndex:1];
        
        
        return cell;
        
        
    }
    

}
//0345 1937





-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        if ([self.type isEqualToString:@"1"]) {
            
            
            if (myViewModel.searchHistory.searchHistoryStrs.count > 0) {
                
                if (indexPath.section == 0) {
                    ZMSearchHeaderVeiw *view = (ZMSearchHeaderVeiw *)[collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                                                                        withReuseIdentifier:[ZMSearchHeaderVeiw identifierStr]
                                                                                                               forIndexPath:indexPath];
                    
                    
                    view.type = @"1";
                    
                    view.isHidenBtn = YES;
                    
                    
                    return view;
                } else {
                    ZMSearchHeaderVeiw *view = (ZMSearchHeaderVeiw *)[collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                                                                        withReuseIdentifier:[ZMSearchHeaderVeiw identifierStr]
                                                                                                               forIndexPath:indexPath];
                    
                    
                    view.isHidenBtn = NO;
                    
                    
                    return view;
                }
                
                
            }else{
                
                ZMSearchHeaderVeiw *view = (ZMSearchHeaderVeiw *)[collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                                                                    withReuseIdentifier:[ZMSearchHeaderVeiw identifierStr]
                                                                                                           forIndexPath:indexPath];
                
                
                view.type = @"1";
                
                view.isHidenBtn = YES;
                
                
                return view;
                
                
            }
            
            
            
            
            
        }  else if ([self.type isEqualToString:@"2"]) {
            
            
            if (myViewModel.searchHistory.searchHistoryStrs.count > 0) {
                
                if (indexPath.section == 0) {
                    ZMSearchHeaderVeiw *view = (ZMSearchHeaderVeiw *)[collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                                                                        withReuseIdentifier:[ZMSearchHeaderVeiw identifierStr]
                                                                                                               forIndexPath:indexPath];
                    
                    
                    view.type = @"2";
                    
                    view.isHidenBtn = YES;
                    
                    
                    return view;
                } else {
                    ZMSearchHeaderVeiw *view = (ZMSearchHeaderVeiw *)[collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                                                                        withReuseIdentifier:[ZMSearchHeaderVeiw identifierStr]
                                                                                                               forIndexPath:indexPath];
                    
                    
                    view.isHidenBtn = NO;
                    
                    
                    return view;
                }
                
                
            }else{
                
                ZMSearchHeaderVeiw *view = (ZMSearchHeaderVeiw *)[collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                                                                    withReuseIdentifier:[ZMSearchHeaderVeiw identifierStr]
                                                                                                           forIndexPath:indexPath];
                
                
                
                view.type = @"2";

                view.isHidenBtn = YES;
                
                
                return view;
                
                
            }
            
            
            
            
            
        } else {
            
            ZMSearchHeaderVeiw *view = (ZMSearchHeaderVeiw *)[collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                                                                withReuseIdentifier:[ZMSearchHeaderVeiw identifierStr]
                                                                                                       forIndexPath:indexPath];
            
            
            view.isHidenBtn = NO;
            
            
            return view;
            
        }
        
        
      
        
    } else {
        
        UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                                            withReuseIdentifier:@"CollectionViewFooterView"
                                                                                   forIndexPath:indexPath];
        
        return view;
        
    }
    
    
}




- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(KScreenW, 44);
}




- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([self.type isEqualToString:@"1"]) {
        
        
        if (myViewModel.searchHistory.searchHistoryStrs.count > 0) {
            
            if (indexPath.section == 0) {
                
                NSString  *textStr = myViewModel.parameterModel.yellowPagesClass[indexPath.item].name;
                
                CGSize size = [textStr sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:MYFONT(12), NSFontAttributeName, nil]];
                
                return CGSizeMake(size.width + 12,26);
                
            } else {
                
                NSString  *textStr = myViewModel.searchHistory.searchHistoryStrs[indexPath.item];
                
                textStr = [textStr substringFromIndex:1];
                
                CGSize size = [textStr sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:MYFONT(12), NSFontAttributeName, nil]];
                
                return CGSizeMake(size.width + 12,26);
                
            }
            
            
        }else{
            
            NSString  *textStr = myViewModel.parameterModel.yellowPagesClass[indexPath.item].name;
            
            CGSize size = [textStr sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:MYFONT(12), NSFontAttributeName, nil]];
            
            return CGSizeMake(size.width + 12,26);
            
            
        }
        
    } else  if ([self.type isEqualToString:@"2"]) {
        
        
        if (myViewModel.searchHistory.searchHistoryStrs.count > 0) {
            
            if (indexPath.section == 0) {
                
                NSString  *textStr = myViewModel.parameterModel.positionClass[indexPath.item].name;
                
                CGSize size = [textStr sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:MYFONT(12), NSFontAttributeName, nil]];
                
                return CGSizeMake(size.width + 12,26);
                
            } else {
                
                NSString  *textStr = myViewModel.searchHistory.searchHistoryStrs[indexPath.item];
                
                textStr = [textStr substringFromIndex:1];
                
                CGSize size = [textStr sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:MYFONT(12), NSFontAttributeName, nil]];
                
                return CGSizeMake(size.width + 12,26);
                
            }
            
            
        }else{
            
            NSString  *textStr = myViewModel.parameterModel.positionClass[indexPath.item].name;
            
            CGSize size = [textStr sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:MYFONT(12), NSFontAttributeName, nil]];
            
            return CGSizeMake(size.width + 12,26);
            
            
        }
        
    } else {
        
        
        NSString  *textStr = myViewModel.searchHistory.searchHistoryStrs[indexPath.item];
        
        textStr = [textStr substringFromIndex:1];
        
        CGSize size = [textStr sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:MYFONT(12), NSFontAttributeName, nil]];
        
        return CGSizeMake(size.width + 12,26);
        
    }
    

    
}






#pragma mark <UICollectionViewDelegate>

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    
    [self.view endEditing:YES];
}



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
        [self.view endEditing:YES];
    
    
    
    if ([self.type isEqualToString:@"1"]) {
        
        if (indexPath.section == 0) {
            
            
            SearchYellowPaygVC *contr = [[SearchYellowPaygVC alloc]init];
            
            contr.keyword = myViewModel.parameterModel.yellowPagesClass[indexPath.item].name;
            
            [self.navigationController pushViewController:contr animated:YES];
            
            return;
        }
        
    }
    
    
    if ([self.type isEqualToString:@"2"]) {
        
        if (indexPath.section == 0) {
            
            
            SearchEmploymentVC *contr = [[SearchEmploymentVC alloc]init];
            
            contr.keyword = myViewModel.parameterModel.positionClass[indexPath.item].name;
            
            [self.navigationController pushViewController:contr animated:YES];
            
            return;
        }
        
    }
    
        NSString *textStr0 = myViewModel.searchHistory.searchHistoryStrs[indexPath.item];
    
    
        NSString *textStr = [textStr0 substringToIndex:1];
    
    
       NSString *textStr1 = [textStr0 substringFromIndex:1];


        self.headerView.searchTextField.text = textStr1;
    
    
    //1 黄页  2 招聘  3 商品 4租房
    if ([textStr isEqualToString:@"1"]) {
        
        
        SearchYellowPaygVC *contr = [[SearchYellowPaygVC alloc]init];
        
        contr.keyword = textStr1;
        
        [self.navigationController pushViewController:contr animated:YES];
        
        
    } else if ([textStr isEqualToString:@"2"]){
        
        SearchEmploymentVC *contr = [[SearchEmploymentVC alloc]init];
        
        contr.keyword = textStr1;
        
        [self.navigationController pushViewController:contr animated:YES];
        
    } else if ([textStr isEqualToString:@"3"]){
        
        
        SearchCategoryListVC *contr = [[SearchCategoryListVC alloc]init];
        
        contr.keyword = textStr1;
        
        contr.navigationItem.title = textStr1;
        
        [self.navigationController pushViewController:contr animated:YES];
        
        
    }else  if ([textStr isEqualToString:@"4"]){
        
        SearchRentHouseVC *contr = [[SearchRentHouseVC alloc]init];
        
        contr.keyword = textStr1;
        
        contr.navigationItem.title = textStr1;
        
        [self.navigationController pushViewController:contr animated:YES];
        
    }else  if ([textStr isEqualToString:@"5"]){
        
        SearchLifeController *contr = [[SearchLifeController alloc]init];
        
        contr.keyword = textStr1;
        
        contr.navigationItem.title = textStr1;
        
        [self.navigationController pushViewController:contr animated:YES];
        
    }else{
        
        BaseViewController *contr = [[BaseViewController alloc]init];
        
        
        [self.navigationController pushViewController:contr animated:YES];
        
        
    }
    
        
    
    

    
}











- (UIStatusBarStyle)preferredStatusBarStyle{
    
    
    return UIStatusBarStyleLightContent;
    
}

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
